package main

import (
	"github.com/gin-gonic/gin"
	"log"
)

func middlewareTest1(c *gin.Context) {
	log.Println("middlewareTest1 in...")
	c.Set("key", 999)
	log.Println("middlewareTest1...")
	log.Println("middlewareTest1 in before next...")
	//这里会先去执行一遍方法, 方法执行完成后, 再继续执行一次 middleware

	//before request
	c.Next()
	//after request

	log.Println("middlewareTest1 in after next...")
	log.Println("middlewareTest1 done...")
	c.JSON(200, gin.H{
		"message": "middlewareTest1",
		"data":    c.GetInt("key"),
	})
}

func main() {

	//创建一个 gin 的默认实例
	r := gin.Default()

	//匿名函数实现
	r.GET("/ping", middlewareTest1, func(c *gin.Context) {
		k := c.GetInt("key")
		log.Println("function in, k=", k)
		c.Set("key", k+101)

		log.Println("function done, k=", k)
	})

	//默认值为 8080
	//基于http, :port 进行修改
	r.Run(":8080")
}
